//
//  ViewController.m
//  LearningTableAnimation
//
//  Created by Misheral on 15/7/6.
//  Copyright (c) 2015年 lifecycle. All rights reserved.
//

#import "ViewController.h"

#import "TableViewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *table;

@property (nonatomic) NSInteger direction;
@property (nonatomic) CGPoint lastOffset;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.translucent = NO;
    self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    self.table.backgroundColor = [UIColor lightGrayColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 50;
}

//- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return indexPath.row;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 88;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:nil options:nil];
        
        cell = array.firstObject;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    UIPanGestureRecognizer *pan = tableView.panGestureRecognizer;
    CGFloat location = 100;
    if (self.direction == -1) {
        location = -100;
    }
    cell.transform = CGAffineTransformIdentity;
    CGAffineTransform transform;
    transform = CGAffineTransformMakeTranslation(0, location);
    cell.transform = transform;
    
    [UIView beginAnimations:@"transform" context:NULL];
    [UIView setAnimationDelay:0.33];
    cell.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.lastOffset = scrollView.contentOffset;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint point = scrollView.contentOffset;
    
    if (point.y >= self.lastOffset.y) {
        self.direction = 1;
    }else{
        self.direction = -1;
    }
    self.lastOffset = point;
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    self.lastOffset = CGPointZero;
}

@end
